const express = require("express");
const roleList = require("../data/roleData.json");
const menuList = require("../data/menuData.json");
const roleMenu = require("../data/roleMenu.json");
const roleControl = express.Router();

// 获取所有角色列表
roleControl.get("/", function (req, res) {
  //   console.log(req.query);
  const filterRoleList = roleList.filter(
    (item) => item.roleName.indexOf(req.query.searchName) !== -1
  );

  const current = req.query.page;
  const size = req.query.limit;
  const start = (current - 1) * size;
  const end = current * size;
  // console.log(start, end);
  const activeRoleList = filterRoleList.filter((m) => !m.del).reverse();
  const resRoleList = activeRoleList.slice(start, end);
  return res.send({
    code: 200,
    message: "成功",
    data: {
      records: resRoleList,
      total: activeRoleList.length,
      size,
      current,
      orders: [],
      optimizeCountSql: true,
      hitCount: false,
      countId: null,
      maxLimit: null,
      searchCount: true,
      pages: current,
    },
    ok: true,
  });
});
// 新增角色
roleControl.post("/save", function (req, res) {
  //   console.log(req.body);
  const id = roleList[roleList.length - 1].id + 1;
  roleList.push({
    id,
    createTime: new Date().toLocaleString(),
    updateTime: "",
    roleName: req.body.roleName,
    remark: null,
  });
  return res.send({
    code: 200,
    message: "成功",
    data: {},
    ok: true,
  });
});
// 修改角色
roleControl.put("/update", function (req, res) {
  //   console.log(req.body);
  const role = roleList.find((m) => m.id == req.body.id);
  if (!role) {
    return res.send({
      code: 201,
      message: "找不到该角色",
      data: {},
      ok: true,
    });
  }
  role.roleName = req.body.roleName;
  role.updateTime = new Date().toLocaleString();
  return res.send({
    code: 200,
    message: "成功",
    data: {},
    ok: true,
  });
});
// 删除角色
roleControl.get("/remove", function (req, res) {
  // console.log(req.query);
  let flag = false;
  roleList.forEach((item) => {
    if (req.query.id == item.id) {
      item.del = true;
      flag = true;
    }
  });
  if (flag) {
    return res.send({
      code: 200,
      message: "成功",
      data: {},
      ok: true,
    });
  } else {
    return res.send({
      code: 201,
      message: "未找到角色！",
      data: {},
      ok: true,
    });
  }
});

// 获取角色对应菜单列表
roleControl.get("/permissions", function (req, res) {
  // console.log(req.query.id);
  const ids = roleMenu.find((m) => m.roleId == req.query.id);

  // console.log(ids);

  let resMenuList = JSON.parse(JSON.stringify(getMenuList(menuList)));
  if (ids) {
    resMenuList = setSelect(ids.menuId, resMenuList);
  }

  return res.send({
    code: 200,
    message: "成功",
    data: resMenuList,
    ok: true,
  });
});
// 设置角色对应的菜单
roleControl.post("/setPermissions", function (req, res) {
  // console.log(req.body);
  const ids = roleMenu.find((m) => m.roleId == req.body.roleId);
  if (ids) {
    ids.menuId = req.body.menuId;
  } else {
    roleMenu.push(req.body);
  }
  return res.send({
    code: 200,
    message: "成功",
    data: {},
    ok: true,
  });
});

module.exports = roleControl;

// 用于设置角色对应的菜单
const setSelect = (menuId, menus) => {
  menus.forEach((m) => {
    if (m.children.length > 0) {
      m.children = setSelect(menuId, m.children);
    }
    if (menuId.includes(m.id)) {
      m.selected = true;
    } else {
      m.selected = false;
    }
  });
  return menus;
};
// 获取所有菜单 过滤del标志
const getMenuList = (list) => {
  let lv1 = [],
    lv2 = [],
    lv3 = [],
    lv4 = [];

  list.forEach((item) => {
    if (!item.del) {
      switch (item.level) {
        case 1:
          lv1.push(item);
          break;
        case 2:
          lv2.push(item);
          break;
        case 3:
          lv3.push(item);
          break;
        case 4:
          lv4.push(item);
          break;
        default:
          break;
      }
    }
  });
  lv3.forEach((item) => {
    let children = lv4.filter((m) => m.pid === item.id);
    item.children = children;
  });
  lv2.forEach((item) => {
    let children = lv3.filter((m) => m.pid === item.id);
    item.children = children;
  });
  lv1.forEach((item) => {
    let children = lv2.filter((m) => m.pid === item.id);
    item.children = children;
  });

  return lv1;
};
